package com.example.bledemo.net;

import android.util.Log;

import com.example.bledemo.MyApplication;
import com.example.bledemo.util.IpSettingsManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Peter_Z.
 * <p>
 * Date: 2025/8/5 0005
 */
public class RetrofitClient {
//    private static final String BASE_URL = "http://your.server.ip:port/"; // 可动态设置
    private static Retrofit retrofit;

    public static Retrofit getInstance() {
        String BASE_URL = IpSettingsManager.getBaseUrl(MyApplication.getContext());
        if (retrofit == null) {
            synchronized (RetrofitClient.class) {
                if (retrofit == null) {
                    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message ->
                            Log.d("Retrofit", message));
                    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);  // 打印请求头/体:ml-citation{ref="2,4" data="citationList"}

                    OkHttpClient client = new OkHttpClient.Builder()
                            .addInterceptor(new CustomLogInterceptor())
                            .build();

                    Gson gson = new GsonBuilder().setLenient().create();

                    retrofit = new Retrofit.Builder()
                            .baseUrl(BASE_URL)
                            .client(client)
                            .addConverterFactory(GsonConverterFactory.create(gson))
                            .build();
                }
            }
        }
        return retrofit;
    }

    public static ApiService getApiService() {
        return getInstance().create(ApiService.class);
    }

}